﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace folderRename
{
    public partial class Form1 : Form
    {

        private bool isFolder = true;
        private int renameFileCount = 0;
        private string fileNameExp = null;
        private string rootFolder = null;
        private string oldPart = null;
        private string newPart = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr= folderBrowserDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                this.textBox2.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //if (textBox1.Text.Length == 0)
            //{
            //    MessageBox.Show("请输入要进行重命名的文件夹名称正则表达式！");
            //    return;
            //}
            if (radioButton1.Checked)
            {
                isFolder = false;
            }

            renameFileCount = 0;

            if (textBox2.Text.Length == 0)
            {
                MessageBox.Show("请选择要进行重命名的文件夹！");
                return;
            }

            if (textBox3.Text.Length == 0)
            {
                MessageBox.Show("请输入需替换部分！");
                return;
            }


            fileNameExp = textBox1.Text;
            rootFolder = textBox2.Text;
            oldPart = textBox3.Text;
            newPart = textBox4.Text;
            DirectoryInfo dInfo= new DirectoryInfo(rootFolder);
            FileSystemInfo finfo =  dInfo as FileSystemInfo;
            renameFile(finfo);

            if (renameFileCount > 0)
            {
                MessageBox.Show("完成" + renameFileCount + "个" + (isFolder ? "文件夹" : "文件") + "重命名！");
            }
            else
            {
                MessageBox.Show("没有符合条件的" + (isFolder ? "文件夹" : "文件") + "！");
            }
        }

        /// <summary>
        /// 删除指定文件夹下的指定名称的文件或文件夹
        /// </summary>
        /// <param name="info"></param>
        /// <param name="rootPath"></param>
        /// <returns></returns>
        private bool renameFile(FileSystemInfo info)
        {
            bool ret = false;
            if (!info.Exists)
            {
                return ret;
            }
            DirectoryInfo dir = info as DirectoryInfo;
            //不是目录
            if (dir == null)
            {
                return ret;
            }

            FileSystemInfo[] files = dir.GetFileSystemInfos();
            try
            {
                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = files[i] as FileInfo;
                    //是文件
                    if (file != null)
                    {
                        if (isFolder)
                        {
                            
                        }
                        else
                        {
                            if ((null == fileNameExp || fileNameExp.Length == 0) || ((null != fileNameExp && fileNameExp.Length > 0) && (isRightFile(fileNameExp, file.Name) || file.Name.IndexOf(fileNameExp) >= 0)))
                            {
                                if (file.Name.IndexOf(oldPart) >= 0)
                                {
                                    string newName = file.DirectoryName + "\\" + file.Name.Replace(oldPart, newPart);
                                    file.MoveTo(newName);
                                    renameFileCount++;
                                }
                            }
                        }

                    }
                    //对于子目录，进行递归调用
                    else
                    {
                        DirectoryInfo subDir = files[i] as DirectoryInfo;
                        FileSystemInfo[] subdirs = subDir.GetFileSystemInfos();
                        if (isFolder)
                        {
                            if ((null == fileNameExp || fileNameExp.Length == 0) || ((null != fileNameExp && fileNameExp.Length > 0) && (isRightFile(fileNameExp, subDir.Name) || subDir.Name.IndexOf(fileNameExp) >= 0)))
                            {
                                if (subDir.Name.IndexOf(oldPart) >= 0)
                                {
                                    string newName = subDir.Parent.FullName + "\\" + subDir.Name.Replace(oldPart, newPart);
                                    subDir.MoveTo(newName);
                                    renameFileCount++;
                                }
                            }
                        }

                        if ((subDir.GetDirectories().Length == 0 && isFolder) || (subdirs.Length == 0 && !isFolder))
                        { 

                        }
                        else
                        {
                            renameFile(files[i]);
                        }
                    }
                }
                ret = true;
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return ret;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        //判断文件/文件夹名称是否符合正则表达式
        public static bool isRightFile(string exp,string fileName)
        {
            Regex rg = new Regex(exp);
            return rg.IsMatch(fileName);
        }
    }
}
